package com.haiyou.sms.alibaba.config;

import com.haiyou.common.constant.RedisKeys;
import com.haiyou.common.util.RedisUtil;
import com.haiyou.sms.alibaba.properties.AliSmsProperties;
import com.haiyou.sms.alibaba.service.AliSmsService;
import com.haiyou.sms.alibaba.service.impl.AliSmsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(value = AliSmsProperties.class)
@ConditionalOnProperty(prefix = AliSmsProperties.PREFIX, name = "enable", havingValue = "true")
public class AliSmsAutoConfiguration {

    @Bean
    public RedisUtil redisUtil() {
        return new RedisUtil();
    }

    @Bean
    public AliSmsService aliSmsService(AliSmsProperties properties) {
        log.info("--->阿里云短信配置：{}", properties);
        RedisKeys.PREFIX = properties.redisPrefix;
        return new AliSmsServiceImpl(properties, redisUtil());
    }

}
